package com.tz.tzgw.pc.web.dao;

import com.tz.tzgw.pc.web.model.Banner;
import com.tz.tzgw.pc.web.model.Member;
import com.tz.tzgw.pc.web.model.PageResult;
import com.tz.tzgw.pc.web.model.Webcolumn;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

public class BannerDao {
    private final JdbcTemplate jdbcTemplate;

    public BannerDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    public PageResult<Banner> getBannersByPageAndCondition(int pageNum, int pageSize, String title) {
        PageResult<Banner> pageResult = new PageResult<>();
        int offset = (pageNum - 1) * pageSize;

        // 构建查询总条数的 SQL
        StringBuilder countSqlBuilder = new StringBuilder("SELECT COUNT(*) FROM tz_banner");
        List<Object> countParams = new ArrayList<>();

        // 构建分页查询的 SQL
        StringBuilder querySqlBuilder = new StringBuilder("SELECT * FROM tz_banner");
        List<Object> queryParams = new ArrayList<>();

        // 判断 title 是否为空，并构建相应的查询条件
        if (title != null && !title.isEmpty()) {
            countSqlBuilder.append(" WHERE title LIKE ?");
            countParams.add("%" + title + "%");

            querySqlBuilder.append(" WHERE title LIKE ?");
            queryParams.add("%" + title + "%");
        }

        // 查询总条数
        String countSql = countSqlBuilder.toString();
        int total = jdbcTemplate.queryForObject(countSql, Integer.class, countParams.toArray());
        pageResult.setTotal(total);

        // 分页查询
        querySqlBuilder.append(" ORDER BY sort_order DESC LIMIT ?, ?");
        queryParams.add(offset);
        queryParams.add(pageSize);

        String querySql = querySqlBuilder.toString();
        List<Banner> banners = jdbcTemplate.query(querySql, new BeanPropertyRowMapper<>(Banner.class), queryParams.toArray());
        pageResult.setData(banners);

        return pageResult;
    }


    public int insertOrUpdateBanner(Banner banner) {
        if (banner.getId() != null) {
            String sql = "UPDATE tz_banner SET title = ?, sub_title = ?, thumb = ?, mthumb = ?, url = ?, murl = ?, sort_order = ? WHERE id = ?";
            jdbcTemplate.update(sql, banner.getTitle(), banner.getSub_title(), banner.getThumb(), banner.getMthumb(), banner.getUrl(), banner.getMurl(), banner.getSort_order(), banner.getId());
        } else {
            String sql = "INSERT INTO tz_banner (title, sub_title, thumb, mthumb, url, murl, sort_order) VALUES (?, ?, ?, ?, ?, ?, ?)";
            jdbcTemplate.update(sql, banner.getTitle(), banner.getSub_title(), banner.getThumb(), banner.getMthumb(), banner.getUrl(), banner.getMurl(), banner.getSort_order());
        }
        return  1;
    }


    public List<Banner> getAllBanners() {
        String sql = "SELECT * FROM tz_banner WHERE status=1 ORDER BY sort_order DESC ";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Banner.class));
    }

    public Integer deleteBanner(Integer id) {
        String sql = "DELETE FROM tz_banner WHERE id = ?";
        jdbcTemplate.update(sql, id);
        return 1;
    }
}
